package com.example.myapplication.event

import android.view.GestureDetector
import android.view.MotionEvent
import java.lang.Math.abs

/**
 * @auth: njb
 * @date: 2024/1/11 0:10
 * @desc: 描述
 */
class CustomGestureListener (private val onThreeFingerSwipeDown: () -> Unit) : GestureDetector.SimpleOnGestureListener() {
    private companion object {
        const val SWIPE_THRESHOLD = 100
        const val SWIPE_VELOCITY_THRESHOLD = 100
    }

    override fun onFling(event1: MotionEvent, event2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
        val pointerCount = event1.pointerCount

        if (pointerCount == 3 && event2.pointerCount == 3) {
            val diffY = event2.y - event1.y
            val diffX = event2.x - event1.x

            if (abs(diffY) > abs(diffX)
                && abs(diffY) > SWIPE_THRESHOLD
                && abs(velocityY) > SWIPE_VELOCITY_THRESHOLD
                && diffY > 0
            ) {
                onThreeFingerSwipeDown.invoke() // 执行退出界面的逻辑
                return true
            }
        }

        return false
    }
}